<template>
    <div id="loginBox">
        <h2>快速注册</h2>
        <div id="login_form">

            <el-form :model="zhuceUserForm" ref="ruleForm"  :rules="editRules" label-width="80px" class="demo-ruleForm">
                <el-form-item label="上传头像"  prop="url"  >
                    <el-upload
                            class="avatar-uploader"
                            action="http://192.168.1.37:7000/upload/upload"
                            :show-file-list="false"
                            :on-success="handleAvatarSuccess"
                    >
                        <img v-if="zhuceUserForm.url" :src="zhuceUserForm.url" class="avatar">
                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>
                </el-form-item>

                <el-form-item   label-width="65px" prop="roleid">
                    <el-input  type="hidden" v-model="zhuceUserForm.roleid" ></el-input>
                </el-form-item>
                <el-form-item  label-width="65px" prop="rolename">
                    <el-input type="hidden" v-model="zhuceUserForm.rolename" ></el-input>
                </el-form-item>
                <el-form-item label="用户名" label-width="65px" prop="username">
                    <el-input  v-model="zhuceUserForm.username" autocomplete="off" onkeyup="this.value=this.value.replace(/\s+/g,'')"></el-input>
                </el-form-item>
                <el-form-item label="密码" label-width="65px" prop="password">
                    <el-input v-model="zhuceUserForm.password" autocomplete="off" onkeyup="this.value=this.value.replace(/\s+/g,'')"></el-input>
                </el-form-item>
<!--                <el-form-item label="餐厅名" label-width="65px">-->
<!--                    <el-select v-model="zhuceUserForm.rid" placeholder="选择饭店">-->
<!--                        <el-option v-for="kind in kinds" :label="kind.restaurantName" :value="kind.rid"></el-option>-->
<!--                    </el-select>-->
<!--                </el-form-item>-->
                <el-form-item label="昵称" label-width="65px" prop="usermonicker">
                    <el-input v-model="zhuceUserForm.usermonicker" autocomplete="off" onkeyup="this.value=this.value.replace(/\s+/g,'')"></el-input>
                </el-form-item>

                <el-form-item label="手机号" label-width="65px" prop="phone">
                    <el-input v-model="zhuceUserForm.phone" autocomplete="off" oninput="value=value.replace(/[^\d]/g,'');if(value.length > 11)value=value.slice(0,11)"> </el-input>
                </el-form-item>
                <el-form-item label="验证码" label-width="65px" prop="msg">
                    <el-input style="width: 100px" v-model="zhuceUserForm.msg" autocomplete="off" onkeyup="this.value=this.value.replace(/\s+/g,'')"> </el-input>
                   &nbsp&nbsp&nbsp&nbsp&nbsp
                    <el-button type="primary" @click="sendMsg" id="button">{{goYan}}</el-button>
                </el-form-item>
                <el-form-item>
            <span >
                <el-button @click="remark">取 消</el-button>&nbsp&nbsp
                <el-button type="primary" @click="gozhuce">确 定</el-button>
            </span>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
    export default {
        name: "Zhuce",
        data(){
            return{
                //所有的饭店信息
                kinds:[],
                goYan:"发送验证码",
                //验证码
                random:"",
                //注册表单
                zhuceUserForm:{
                    roleid:"3",
                    rolename:"用户"
                },
                //校验规则
                editRules: {
                    url:[
                         {required: true, message: '请设置头像', trigger: 'blur'},
                    ],
                    username: [
                        {required: true, message: '请设置用户名', trigger: 'blur'},
                        {max:12,min:6, message: '用户名为6-12位', trigger: 'blur'},
                    ],
                    usermonicker: [
                        {required: true, message: '请设置昵称', trigger: 'blur'},
                    ],
                    phone: [
                        {required: true, message: '请输入手机号', trigger: 'blur'},
                        {min:11,message: '手机号格式不正确', trigger: 'blur'},
                        {
                            pattern: /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/,
                            message: '请输入正确的手机号码'
                        }
                    ],
                    password: [
                        {required: true, message: '请设置密码', trigger: 'blur'},
                        {max:12,min:6, message: '密码为6-12位', trigger: 'blur'},
                    ],
                }
            }
        },


        methods:{

            //验证码倒计时
            go(){
                var d1 = document.getElementById("button");
                d1.disabled=true;
                var i=61;
                var b = setInterval(()=>{
                    i--;
                    this.goYan="发送验证码("+i+"s)";
                    if (i===-1)
                    {
                        clearInterval(b);
                        d1.disabled=false;
                        this.goYan="发送验证码"
                    }
                },1000)
            },


            //发送验证码
            sendMsg(){
                this.$refs['ruleForm'].validate((valid) => {
                    if (valid) {//校验通过
                        this.go();
                            this.$http.post("/user/user/sendMsg",this.zhuceUserForm).then(result=>{
                                if (result.data.code===2000) {
                                    this.$message.success(result.data.msg);
                                }
                                this.random=result.data.data;
                        })
                    }
                })

            },

            //添加用户头像成功后触发的函数
            handleAvatarSuccess(res, file) {
                this.zhuceUserForm.url = res.data;
            },


            //返回login界面
            remark(){
                this.$router.push("/login");
            },

            //去后端注册
            gozhuce(){
                if (this.random!=this.zhuceUserForm.msg && this.zhuceUserForm.msg!=null){
                    this.$message.error("验证码有误");
                }
                else if (this.zhuceUserForm.url=="" || this.zhuceUserForm.url==null){
                    this.$message.error("请信息填写完整");
                }
                else{
                    this.$http.post("/user/user/zhuce",this.zhuceUserForm).then(result=>{
                        console.log("uuuu");
                        if (result.data.code===2000){
                            this.$message.success(result.data.msg);
                            this.$router.push("/login");
                        }else{
                            this.$message.error(result.data.msg);
                        }

                    })
                  }
            },

        },
    }
</script>

<style scoped>
    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }
    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }
    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 100px;
        line-height: 178px;
        text-align: center;
    }
    .avatar {
        width: 178px;
        height: 178px;
        display: block;
    }
    #loginBox {
        width: 450px;
        height: 300px;
        background: white;
        border-radius: 3px;
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);
    }
    #login_form{
        position: absolute;
        bottom: 0;
        top: 0px;
        width: 100%;
        padding: 0 20px;
        box-sizing: border-box;
    }
    h2{
        position: absolute;
        top: -100px;
        left: 43%;
    }
</style>